package com.fulihui.information.biz.conv;

import com.fulihui.information.dal.dataobj.InfoContent;
import com.fulihui.information.dto.ContentDTO;
import com.fulihui.information.dto.ContentListItemDTO;

import java.util.List;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;
import static org.springframework.util.CollectionUtils.isEmpty;

/**
 * @author LeeSon on 2016/12/28 0028.
 */
public class ContentConv {

    public static <T extends ContentListItemDTO> T toDTO(InfoContent infoContent, Class<T> clz) {
        if (infoContent == null) {
            return null;
        }

        try {
            T dto = clz.newInstance();
            dto.setId(infoContent.getId());
            dto.setCategoryCode(infoContent.getCategoryCode());
            dto.setTopStatus(infoContent.getTopStatus());
            dto.setSourceCode(infoContent.getSourceCode());
            dto.setUnionCode(infoContent.getUnionCode());
            dto.setTitle(infoContent.getTitle());
            dto.sourceTime(infoContent.getSubmitTime());
            dto.setListImgs(infoContent.getListImgs());
            dto.setListComment(infoContent.getListComment());
            dto.setStatus(infoContent.getStatus());
            dto.setLinkType(infoContent.getLinkType());
            dto.setSrcHref(infoContent.getSrcHref());
            dto.gmtCreate(infoContent.getGmtCreate());
            dto.gmtModified(infoContent.getGmtModified());

            if (infoContent.getReadTimes() != null) {
                dto.setReadTimes(infoContent.getReadTimes());
            }
            if (infoContent.getVoteUp() != null) {
                dto.setUpvote(infoContent.getVoteUp());
            }
            if (infoContent.getVoteDown() != null) {
                dto.setDownvote(infoContent.getVoteDown());
            }
            if (infoContent.getSharedTimes() != null) {
                dto.setShared(infoContent.getSharedTimes());
            }
            if (infoContent.getCommentTimes() != null) {
                dto.setCommentTimes(infoContent.getCommentTimes());
            }
            if (infoContent.getStarTimes() != null) {
                dto.setStarTimes(infoContent.getStarTimes());
            }

            if (dto instanceof ContentDTO) {
                ((ContentDTO) dto).setContents(infoContent.getContents());
                ((ContentDTO) dto).setRead_(infoContent.getSrcRead());
                ((ContentDTO) dto).setVoteUp_(infoContent.getVoteUp());
                ((ContentDTO) dto).setVoteDown_(infoContent.getVoteDown());
                ((ContentDTO) dto).setShared_(infoContent.getSrcShared());
                ((ContentDTO) dto).setSource_(infoContent.getSrcSource());
                ((ContentDTO) dto).setOperator(infoContent.getOperator());
            }
            return dto;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


    public static <T extends ContentListItemDTO> List<T> toList(List<InfoContent> list, Class<T> clz) {
        if (isEmpty(list)) {
            return emptyList();
        }
        return list.stream().map(it -> toDTO(it, clz)).collect(Collectors.toList());
    }
}
